$(function () {
	var userid =  T.p('userGuid')
	vm.getUser(userid);
	//vm.getpic(userid);
	vm.getPicInfo(userid);
	//vm.getpics(userid);
	
	
	new AjaxUpload('#upload', {
        action: baseURL + "frame/attachinfo/upload",
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        data:{'id': userid},
        onSubmit:function(file, extension){
            if (!(extension && /^(jpg|jpeg|png|gif)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、png、gif格式的图片！');
                return false;
            }
        },
        onComplete : function(file, r){
            if(r.code == 0){
                vm.reload();
            }else{
                alert(r.msg);
            }
        }
    });
	
	new AjaxUpload('#upload1', {
        action: baseURL + "frame/attachinfo/upload",
        name: 'file',
        autoSubmit:true,
        responseType:"json",
        data:{'id': userid},
        onSubmit:function(file, extension){
           
        },
        onComplete : function(file, r){
            if(r.code == 0){
                vm.reload();
            }else{
                alert(r.msg);
            }
        }
    });
});



var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "ouguid",
            pIdKey: "parentouguid",
            rootPId: -1
        },
        key: {
            url:"nourl",
            name:"ouname"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        q:{
            loginid: null
        },
        showList: true,
        title:null,
        roleList:{},
        user:{
            status:1,
            ouguid:null,
            ouname:null,
            roleIdList:[]
        }
    },
    methods: {
        update: function () {
            var userguid = getSelectedRow();
            if(userguid == null){
                return ;
            }

            vm.showList = false;
            vm.title = "修改";

            vm.getUser(userguid);
        },
        saveOrUpdate: function () {
            var url = "sys/user/updateuser";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.user),
                success: function(r){
                    if(r.code === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.msg);
                    }
                }
            });
        },
        getUser: function(userId){
            $.get(baseURL + "sys/user/info/"+userId, function(r){
                vm.user = r.user;

            });
        },
        delPic: function(attachguid){
        	
        	//var attachguid = vm.info.attachguid;
            $.get(baseURL + "frame/attachinfo/del/"+attachguid, function(r){//frame/attachinfo/del/
            	vm.reload();
            });
        },
        deleteByClientguid: function(userid){
        	
            $.get(baseURL + "frame/attachinfo/deleteAttachByClientGuid/"+userid, function(r){
            	vm.reload();
            });
        },
        getpic: function(userId){
        	 $.get(baseURL + "frame/attachinfo/getattachname/"+userId, function(r){
        		
                 vm.info = r.info;
                 if(vm.info.attachguid != null){
                 $("#d1").append('<a href="javascript:;" >'+vm.info.attachfilename+'</a><a class="badge bg-red" onclick="vm.delPic(\'' + vm.info.attachguid + '\')">X</a>');
                 
                 
                 }
             });
        },
        getpics: function(userId){
       	 $.get(baseURL + "frame/attachinfo/getattachstorages/"+userId, function(r){
       		
                vm.info = r.info;
                for(var i in vm.info) {
                    $("#d3").append('<a href="'+baseURL+'frame/attachinfo/download?attachguid='+vm.info[i].attachguid+'" >'+vm.info[i].attachfilename+'</a><a class="badge bg-red" onclick="vm.delPic(\'' + vm.info[i].attachguid + '\')">X</a>');
               }
              
            });
       },
       downloadFile: function(fileName,content){
    	   console.log(99);
    	   var $eleForm = $("<form method='get'></form>");  
    	   
           $eleForm.attr("action",baseURL + "frame/attachinfo/getattachstorages/");  
 
           $(document.body).append($eleForm);  
 
           //提交表单，实现下载  
           $eleForm.submit();  
    	 /*  window.open(content);  
    	   // 创建隐藏的可下载链接
    	    var eleLink = document.createElement('a');
    	    eleLink.download = fileName;
    	    eleLink.style.display = 'none';
    	    // 字符内容转变成blob地址
    	    var blob = new Blob([content]);
    	    eleLink.href = URL.createObjectURL(blob);
    	    // 触发点击
    	    document.body.appendChild(eleLink);
    	    eleLink.click();
    	    // 然后移除
    	    document.body.removeChild(eleLink);*/
       },
        getPicInfo: function(userId){
            $.get(baseURL + "frame/attachinfo/getattachstorage/"+userId, function(r){
            	 vm.storage = r.storage;
            	 if(vm.storage != null){
            	 $("#d1").append('<img style="width:90px;height90px;" src=data:image/jpeg;base64,'+vm.storage.content+'></img><a class="badge bg-red" onclick="vm.deleteByClientguid(\'' + userId + '\')">X</a>');
            	 $("#d2").hide();
            	 }
            });
           
        },
        reload: function () {
        	location.reload();
        }
    }
});